package com.tanhua.dubbo.server.api;


import cn.hutool.core.util.IdUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.Method;
import cn.hutool.json.JSONException;
import cn.hutool.json.JSONUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.server.config.HuanXinConfig;
import com.tanhua.dubbo.server.enums.HuanXinMessageType;
import com.tanhua.dubbo.server.mapper.HuanXinUserMapper;
import com.tanhua.dubbo.server.pojo.HuanXinUser;
import com.tanhua.dubbo.server.service.RequestService;
import com.tanhua.dubbo.server.service.TokenService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.Date;

@Service(version = "1.0.0")
@Slf4j
public class HuanXinApiImpl implements HuanXinApi {

    @Autowired
    private TokenService tokenService;
    @Autowired
    private HuanXinConfig huanXinConfig;
    @Autowired
    private RequestService requestService;
    @Autowired
    private HuanXinUserMapper huanXinUserMapper;

    @Override
    public String getToken() {
        return this.tokenService.getToken();
    }

    /**
     * 在环信里面注册用户
     *
     * @param userId 用户id
     * @return
     */
    @Override
    public Boolean register(Long userId) {
        String targetUrl = this.huanXinConfig.getUrl() + this.huanXinConfig + "/" + this.huanXinConfig.getAppName() + "token";
        HuanXinUser huanXinUser = new HuanXinUser();
        huanXinUser.setUsername("HX_" + userId); //用户名
        huanXinUser.setPassword(IdUtil.simpleUUID());//随机生成的密码
        HttpResponse response = this.requestService.execute(targetUrl, JSONUtil.toJsonStr(Arrays.asList(huanXinUser)), Method.POST);
        if (response.isOk()) {
            //注册成功 得到数据 将环信的账号信息保存到数据库
            huanXinUser.setUserId(userId);
            huanXinUser.setCreated(new Date());
            huanXinUser.setUpdated(huanXinUser.getUpdated());
            this.huanXinUserMapper.insert(huanXinUser);
            return true;
        }
        return false;
    }

    /**
     * 查询环信信息 应该在redis中查询
     *
     * @param userId
     * @return
     */
    @Override
    public HuanXinUser queryHuanXinUser(Long userId) {
        QueryWrapper<HuanXinUser> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        HuanXinUser huanXinUser = this.huanXinUserMapper.selectOne(wrapper);
        return huanXinUser;
    }

    /**
     * 根据用户id 查询环信账户信息
     * @param userName
     * @return
     */
    @Override
    public HuanXinUser queryUserInfoByUserName(String userName) {
        QueryWrapper<HuanXinUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",userName);
        HuanXinUser huanXinUser = this.huanXinUserMapper.selectOne(queryWrapper);
        return huanXinUser;
    }

    /**
     * 在环信里面添加好友关系
     * @param userId   自己的id
     * @param friendId 好友的id
     * @return
     */
    @Override
    public Boolean addUserFriend(Long userId, Long friendId) {
        String targetUrl = this.huanXinConfig.getUrl() + this.huanXinConfig.getOrgName() + "/" +
                this.huanXinConfig.getAppName() + "user/HX_" + userId + "/contacts/users/HX_" + friendId;
        try {// 404 -> 对方未在环信注册
          return   this.requestService.execute(targetUrl,null,Method.POST).isOk();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 添加失败
        return false;
    }

    @Override
    public Boolean removeUserFriend(Long userId, Long friendId) {
        String targetUrl = this.huanXinConfig.getUrl() + this.huanXinConfig.getOrgName()
                + "/" + this.huanXinConfig.getAppName() + "users/HX_" + userId + "/contacts/users/HX_" + friendId;
        try {
           return this.requestService.execute(targetUrl,null,Method.DELETE).isOk();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //删除失败
        return false;
    }

    @Override
    public Boolean sendMsgFromAdmin(String targetUserName, HuanXinMessageType huanXinMessageType, String msg) {
        try {
            ///{org_name}/{app_name}/messages   请求路径地址
            String targetUrl = this.huanXinConfig.getUrl() + this.huanXinConfig.getOrgName() + "/" + this.huanXinConfig.getAppName() + "/messages";
            //环信的相互通信请求参数和格式
            String body = JSONUtil.createObj()
                    .set("target_type", "users")
                    .set("target", JSONUtil.createArray().set(targetUserName))
                    .set("msg", JSONUtil.createObj()
                            .set("type", huanXinMessageType.getType())
                            .set("msg", msg)).toString();

            return  this.requestService.execute(targetUrl,body,Method.POST).isOk();
        } catch (JSONException e) {
            log.error("发送消息失败~ targetUserName = " + targetUserName+", type = " + huanXinMessageType.getType()+", msg = " + msg, e);
        }
        return false;
    }
}